package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.orderVo.CourseWebVoOrder;
import com.atguigu.eduservice.client.OrdersClient;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.frontvo.CourseFrontVo;
import com.atguigu.eduservice.entity.frontvo.CourseWebVo;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {

   @Autowired
    private EduCourseService courseService;

   @Autowired
    private EduChapterService chapterService;

   @Autowired
   private OrdersClient ordersClient;


   @PostMapping("getFrontCourseList/{page}/{limit}")
   public R getFrontCourseList(
           @ApiParam(name = "page", value = "当前页码", required = true)
           @PathVariable Long page,

           @ApiParam(name = "limit", value = "每页记录数", required = true)
           @PathVariable Long limit,

           @ApiParam(name = "courseQuery", value = "查询对象", required = false)
           @RequestBody(required = false) CourseFrontVo courseFrontVo
   ){
       Page<EduCourse> pageParam = new Page<>(page, limit);
//       IPage<EduCourse> page1 = courseService.page(pageParam, page1 = {Page@9916} null);
//       System.out.println(page1);
       Map<String,Object>map=courseService.getCourseFrontList(pageParam,courseFrontVo);
       return R.ok().data(map);
   }


//   课程详情方法
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){
//      根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo=courseService.getBaseCourseInfo(courseId);


//      根据课程id查询章节和小节
        List<ChapterVo> chapterVideoByCourseId = chapterService.getChapterVideoByCourseId(courseId);

        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        boolean buyCourse = ordersClient.isBuyCourse(courseId, memberId);


        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideoByCourseId",chapterVideoByCourseId).data("isBuy",buyCourse);
    }

//    根据课程id查询课程信息
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id){
        CourseWebVo courseInfo = courseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo,courseWebVoOrder);
        return courseWebVoOrder;
    }




}
